package com.class3.team.fossilProject.Controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.class3.team.fossilProject.entity.Book;
import com.class3.team.fossilProject.service.TimeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.List;

@RestController
public class SearchTimeController {

    @Autowired
    private TimeService timeService;

    @PostMapping(value = "/searchtime")
    @ResponseBody
    public JSONArray searchtime(HttpServletRequest request) {
        if(request.getContentLength()==0) return null;
        System.out.println(request.toString()+"!!!!!!!!!!!!!!!!!");
        JSONObject data = this.getJSONParam(request);
        System.out.println("0");
        System.out.println(data);
        Double startYear = Double.parseDouble(data.get("start_year").toString());
        System.out.println("startYear"+startYear);
        Double endYear = Double.parseDouble(data.get("end_year").toString());
        System.out.println("Year"+endYear);
        List<Book> result = (List<Book>) this.timeService.findByTime(startYear,endYear);
        return listToArray(result);
    }
//    public String select(String request) {
//        System.out.println(request);
//        return "sucess";
//    }

    private JSONArray listToArray(List<Book> list){
        JSONArray arr = new JSONArray();
        for(int i = 0; i < list.size(); i++ ){
            Book book = list.get(i);
            JSONObject p = new JSONObject();
            JSONObject jo = new JSONObject(true);
            jo.put("type", "Feature");
            // 创建 properties的json对象
            p.put("phylum", book.getPhylum());
            p.put("class", book.getClassName());
            p.put("order", book.getOrderName());
            p.put("family", book.getFamily());
            p.put("genus", book.getGenus());
            p.put("species", book.getSpecies());
            p.put("start_year", book.getStartYear());
            p.put("end_year", book.getEndYear());
            jo.put("properties", p);
            JSONObject geo = new JSONObject(true);
            geo.put("type", "Point");
            // 创建点对应的对象
            String[] location = new  String[2];
            location[0] = book.getModernLongitude();
            location[1] = book.getModernLatitude();
            geo.put("coordinates", location);
            jo.put("geometry", geo);
            arr.add(jo);
        }
        return arr;
    }

    public JSONObject getJSONParam(HttpServletRequest request){
//        System.out.println(request);
        System.out.println("getJSONParam");
        System.out.println(request);
        JSONObject jsonParam = null;
        try {
            // 获取输入流
            BufferedReader streamReader = new BufferedReader(new InputStreamReader(request.getInputStream(), "UTF-8"));

            // 写入数据到Stringbuilder
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = streamReader.readLine()) != null) {
                sb.append(line);
            }
            jsonParam = JSONObject.parseObject(sb.toString());
            //System.out.println(jsonParam.toJSONString());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return jsonParam;
    }

    @RequestMapping(value = "/searchheatmap")
    @ResponseBody
    public JSONArray searchheatmap (){
        System.out.println("YES！");
        List result = this.timeService.searchForHeatmap();
        return listToheatmap(result);
    }

    private JSONArray listToheatmap(List<Book> list){
        JSONArray arr = new JSONArray();
        for(int i = 0; i < list.size(); i++ ){
            Book book = list.get(i);
            JSONObject jo = new JSONObject(true);
            jo.put("lat", book.getModernLatitude());
            jo.put("lng", book.getModernLongitude());
            jo.put("count", book.getCount());
            arr.add(jo);
        }
        return arr;
    }

}
